module src.nus;

private import tango.text.Arguments;
private import tango.io.Stdout;

private alias char[] string;

struct NUS
{
  private:
    string usage = "Usage: nusd titleid [options]\n"
      "\n"
      "Available options are:\n"
      "\n"
      "\t-v, --version\t\t\tTitle version\n"
      "\t-n, --not-wii\t\t\tNot a wii title\n"
      "\t-p, --pack\t\t\tPack WAD file\n"
      "\t-h, -?, --help\t\t\tHelp message\n";
  
  public:
    const string TMD = "tmd";
    const string CETK = "cetk";
    
    string titleVersion = null;
    string titleID;
    
    bool isWiiTitle = true;
    bool pack = true;
    
    string[] tmdContents;
    
    ubyte[] tmdBuf;
    ubyte[] cetkBuf;
    
    bool populate( string[] cmdlArgs )
    {
      auto args = new Arguments();
      
      auto t = args( null ).params( 1 ).required.title( "titleid" );
      auto v = args( "version" ).aliased( 'v' ).smush.params( 1 );
      auto n = args( "not-wii" ).aliased( 'n' );
      auto p = args( "pack" ).aliased( 'p' );
      auto h = args( "help" ).aliased( 'h' ).aliased( '?' ).halt;
      
      if ( args.parse( cmdlArgs ) )
      {
        titleID = t.assigned[ 0 ];
        
        if ( v.set )
          titleVersion = v.assigned[ 0 ];
        
        isWiiTitle = !n.set;
        pack = p.set;
        
        return true;
      }
      
      Stdout.formatln( usage );
      
      if ( !h.set )
        Stdout.formatln( "{0}", args.errors( &Stdout.layout.sprint ) );
      
      return false;
    }
}